package hos.helper

import android.content.Context
import android.content.res.ColorStateList
import android.graphics.drawable.Drawable
import android.graphics.drawable.StateListDrawable
import androidx.annotation.ColorInt
import androidx.annotation.ColorRes
import hos.ksl.resColor
import hos.ksl.resDrawable

/**
 * <p>Title: ColorStateListHelper </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @date : 2024-08-09 15:04
 * @version : 1.0
 */
object StateListHelper {

    @JvmStatic
    @JvmOverloads
    fun color(
        @ColorInt defaultColor: Int,
        @ColorInt selectedColor: Int = defaultColor,
        @ColorInt pressedColor: Int = selectedColor,
        @ColorInt checkedColor: Int = selectedColor,
        @ColorInt focusedColor: Int = selectedColor,
        @ColorInt enabledColor: Int = defaultColor,
    ): ColorStateList {
        return ColorStateList( // 定义不同状态的颜色
            arrayOf<IntArray>(
                // 选中状态
                intArrayOf(android.R.attr.state_selected, android.R.attr.state_enabled),  //  选中状态
                intArrayOf(android.R.attr.state_pressed, android.R.attr.state_enabled),  // 按下状态
                intArrayOf(android.R.attr.state_checked),  // 当CheckBox选中时显示 状态
                intArrayOf(android.R.attr.state_focused),  // 获得焦点时 状态
                intArrayOf(android.R.attr.state_enabled),  // 能使用时 状态
                intArrayOf(),
            ),  // 对应的颜色
            intArrayOf(
                selectedColor,  // 按下时的颜色
                pressedColor,  // 选中时的颜色
                checkedColor,  // 选中时的颜色
                focusedColor,  // 焦点时的颜色
                enabledColor,  // 默认颜色
                defaultColor  // 默认颜色
            )
        )
    }

    @JvmStatic
    @JvmOverloads
    fun colorRes(
        context: Context,
        @ColorRes defaultId: Int,
        @ColorRes selectedId: Int = defaultId,
        @ColorRes pressedId: Int = selectedId,
        @ColorRes checkedId: Int = selectedId,
        @ColorRes focusedId: Int = selectedId,
        @ColorRes enabledId: Int = defaultId,
    ): ColorStateList {
        val defaultColor = context.resColor(defaultId)
        val selectedColor = context.resColor(selectedId)
        val pressedColor = context.resColor(pressedId)
        val checkedColor = context.resColor(checkedId)
        val focusedColor = context.resColor(focusedId)
        val enabledColor = context.resColor(enabledId)
        return color(
            defaultColor,
            selectedColor,
            pressedColor,
            checkedColor,
            focusedColor,
            enabledColor
        )
    }

    @JvmStatic
    fun colorValueOf(@ColorInt color: Int): ColorStateList {
        return ColorStateList.valueOf(color)
    }

    @JvmStatic
    @JvmOverloads
    fun drawableRes(
        context: Context,
        defaultId: Int,
        selectedId: Int = defaultId,
        pressedId: Int = selectedId,
        focusedId: Int = selectedId,
        enabledId: Int = defaultId,
    ): StateListDrawable? {
        val defaultDrawable = context.resDrawable(defaultId) ?: return null
        val selectedDrawable = context.resDrawable(selectedId)
        val pressedDrawable = context.resDrawable(pressedId)
        val focusedDrawable = context.resDrawable(focusedId)
        val enabledDrawable = context.resDrawable(enabledId)
        return drawable(
            defaultDrawable,
            selectedDrawable ?: defaultDrawable,
            pressedDrawable ?: defaultDrawable,
            focusedDrawable ?: defaultDrawable,
            enabledDrawable ?: defaultDrawable,
        )
    }

    @JvmStatic
    @JvmOverloads
    fun drawable(
        defaultDrawable: Drawable,
        selectedDrawable: Drawable = defaultDrawable,
        pressedDrawable: Drawable = selectedDrawable,
        focusedDrawable: Drawable = selectedDrawable,
        enabledDrawable: Drawable = defaultDrawable,
    ): StateListDrawable {
        val drawable = StateListDrawable()
        // View.PRESSED_ENABLED_STATE_SET
        drawable.addState(
            intArrayOf(android.R.attr.state_pressed, android.R.attr.state_enabled),
            pressedDrawable
        )
        // View.ENABLED_FOCUSED_STATE_SET
        drawable.addState(
            intArrayOf(android.R.attr.state_enabled, android.R.attr.state_focused),
            focusedDrawable
        )
        // View.FOCUSED_STATE_SET
        drawable.addState(intArrayOf(android.R.attr.state_focused), focusedDrawable)
        // View.ENABLED_STATE_SET
        drawable.addState(intArrayOf(android.R.attr.state_enabled), enabledDrawable)
        // View.EMPTY_STATE_SET
        drawable.addState(intArrayOf(), defaultDrawable)
        return drawable
    }
}